﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace XFramework
{
    /// <summary>
    /// 缓存池管理类
    /// </summary>
    public class PoolManager : Singleton<PoolManager>
    {
        //字典（对象池名--对象池实例）
        private Dictionary<string, Pool> poolList;

        //构造函数
        public PoolManager()
        {
            poolList = new Dictionary<string, Pool>();
        }

        //根据对象池名，使用对应对象池中的拿取物体方法
        public GameObject Pop(string poolName, string objName, Transform parent, Vector3 pos)
        {
            if (!poolList.ContainsKey(poolName))
            {
                poolList.Add(poolName, new Pool());
            }

            return poolList[poolName].Pop(objName, parent, pos);
        }

        //根据对象池名，使用对应对象池中的回收物体方法
        public void Push(string poolName, GameObject obj)
        {
            if (!poolList.ContainsKey(poolName))
            {
                poolList.Add(poolName, new Pool());
            }

            poolList[poolName].Push(obj);
        }

        //清空字典以及每个对象池实例的字典
        public void Clear()
        {
            foreach (Pool pool in poolList.Values)
            {
                pool.PoolClear();
            }

            poolList.Clear();
        }
    }
}